package com.dao.impl;

import com.dao.ProductDAO;
import com.entity.Product;
import com.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
 *
 *@author XCH
 *@date2024 - 11 - 26
 *
 */
public class ProductDAOImpl implements ProductDAO {
    public int addProduct(Product product) {
        int result = 0;
        String sql = "INSERT INTO product (product_id, product_name, product_type, product_price, product_count, product_image, product_date, product_desc, product_sale, product_status) VALUES (?,?,?,?,?,?,?,?,?,?)";
        Connection conn = null;
        PreparedStatement pst = null;

        try {
            conn = DbUtil.getConnection();
            pst = conn.prepareStatement(sql);

            // 设置各个字段
            pst.setString(1, product.getId());
            pst.setString(2, product.getName());
            pst.setString(3, product.getType());
            pst.setDouble(4, product.getPrice());
            pst.setInt(5, product.getCount());
            pst.setString(6, product.getImage());

            // 打印日期，检查传入的值
            java.sql.Date date = product.getDate();
            System.out.println("商品上架日期: " + date);

            pst.setDate(7, date);

            pst.setString(8, product.getDesc());
            pst.setDouble(9, product.getSale());
            pst.setInt(10, product.getStatus());

            result = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(null, pst, conn);
        }
        return result;
    }


    @Override
    public int deleteProduct(String id) {
        int result = 0;
        String sql = "DELETE FROM product WHERE product_id = ?";
        try {
            Connection conn = DbUtil.getConnection();
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, String.valueOf(id));
            result = pst.executeUpdate();
        } catch (SQLException e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateProduct(Product product) {
        int result = 0;
        String sql = "UPDATE product SET product_name = ?, product_type = ?, product_price = ?, product_count = ?, product_image = ?, product_date = ?, product_desc = ?, product_sale = ?, product_status = ? WHERE product_id = ?";

        // 获取数据库连接
        try (Connection conn = DbUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {

            // 设置 PreparedStatement 参数
            pst.setString(1, product.getName());
            pst.setString(2, product.getType());
            pst.setDouble(3, product.getPrice());
            pst.setInt(4, product.getCount());
            pst.setString(5, product.getImage());

            // 确保将 java.util.Date 转换为 java.sql.Date
            if (product.getDate() != null) {
                pst.setDate(6, new java.sql.Date(product.getDate().getTime()));
            } else {
                pst.setNull(6, java.sql.Types.DATE);  // 如果日期为空，则设置为 SQL NULL
            }

            pst.setString(7, product.getDesc());
            pst.setDouble(8, product.getSale());
            pst.setInt(9, product.getStatus());
            pst.setString(10, product.getId());

            // 执行更新操作
            result = pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();  // 打印异常信息，便于调试
        }
        return result;
    }

    @Override
    public List<Product> selectProduct() {
        List<Product> list = new ArrayList<>();
        DbUtil dbUtil = new DbUtil();
        String sql = "select * from product";
        PreparedStatement pst = null;
        try {
            Connection conn = dbUtil.getConnection();
            pst = conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()){
                Product product = new Product();
                product.setId(rst.getString("product_id"));
                product.setName(rst.getString("product_name"));
                product.setType(rst.getString("product_type"));
                product.setPrice(rst.getDouble("product_price"));
                product.setCount(rst.getInt("product_count"));
                product.setImage(rst.getString("product_image"));
                product.setDate(rst.getDate("product_date"));
                product.setDesc(rst.getString("product_desc"));
                product.setSale(rst.getInt("product_sale"));
                product.setStatus(rst.getInt("product_status"));
                list.add(product);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtil.close(null,pst,null);
        }
        return list;
    }
}
